use strict;
use Net::FTP::Recursive;
use Benchmark;
use Encode;


our ($HOMEDIR, $SITECFG, $listonly, $nolist);
our ($site_ID);

#Get directory list file from the site.
sub get_site_list{
	my ($site, $port, $username, $passwd, $dir) = @_;
	my $timestamp1 = new Benchmark;
	Encode::from_to($dir, 'utf8', 'euc-cn');
	
	my $home_dir = check_create($site, $username, $dir);
	my $filename = $dir;
	
	return undef if(-s "$home_dir$filename.tmp.old" && $listonly);
	return "$home_dir$filename.tmp" if(-s "$home_dir$filename.tmp" && $nolist);
	
	#connect and login
	my $ftp = Net::FTP::Recursive->new("$site", timeout => 120, Debug => 0, Port => $port)
  		or print LOG "Could not connect to server.\n" and return undef;
	$ftp->login($username, $passwd) 
		or return undef;
		
	print LOG "Login into $site sucessfully @ ".localtime()."\n";
	$ftp->cwd($dir) or return;
	
	our $T;
	open ($T, ">$home_dir$filename.tmp") or 
		die "Cannot create file $home_dir$filename.tmp.\n";
	$ftp->rdir(Filehandle=>$T);
	$ftp->quit;
	close($T);
	my $timestamp2 = new Benchmark;
	my $timedifference = timediff($timestamp2, $timestamp1);
	print LOG "Logoff from $site @ ".localtime()."\n";
	print LOG "Get list for $site in". timestr($timedifference)."s.\n";
	return "$home_dir$filename.tmp";
}

#Check if a directory has been created under $HOMEDIR/data for a specified site.
sub check_create{
	my ($site, $username, $dir) = @_;
	my @sub_dir = split(/\//, $dir);
	my $dir = "$HOMEDIR/data/$site";
	my $to_make_dir;
	
	mkdir($dir) unless -e ($dir.=$username);
	pop @sub_dir;
	$to_make_dir = $dir;
	foreach(@sub_dir){
		next if $_ eq "";
		mkdir($to_make_dir) unless -e ($to_make_dir.="/".$_);
	}
	return $dir;
}

sub log_fail{
	my $error_type = shift;
	my $site = shift;
	
	if($error_type eq "RES-NORMAL"){
		my ($resource, $res_url, $res_need_tag, $res_size);
		print LOGERR "$error_type:$site\[ID:$site_ID\]$resource\t in $res_url ".
			"\[NEED-TAG:$res_need_tag\]\[SIZE:$res_size\]\n";
	}elsif($error_type eq "RES-FILES"){
		my ($resource, $urll, $res_size) = @_;
		print LOGERR "$error_type:$site\[ID:$site_ID\]$resource\t in $urll ".
			"\[SIZE:$res_size\]\n";
	}elsif($error_type eq "NewItem"){
		my ($item, $parent_url, $size, $dt, $is_dir) = @_;
		print LOGERR "$error_type:$site\[ID:$site_ID\]$item\t in $parent_url ".
			"\[SIZE:$size\]\[DIR:$is_dir\]\[DATE:$dt\]\n";
	}
}

1;